package alingrad.engine.src;

import java.io.IOException;

import org.lwjgl.opengl.Display;

import alingrad.engine.src.graphics.Sprite;
import alingrad.engine.src.graphics.Texture;
import alingrad.engine.src.graphics.TextureLoader;

/**
 * Class that contains all resources of the game.
 * Also implements ways to load the resources.
 * @author alingrad
 *
 */
public class Resources {
	/**
	 * Class that loads the textures.
	 */
	private static TextureLoader textureLoader;

	/**
	 * Constructs the Resource Holder.
	 */
	public Resources() {
		textureLoader = new TextureLoader();
	}

	/**
	 * Assigns a texture to a sprite and returns the sprite.
	 * @param res location of the texture to find.
	 * @return the sprite.
	 */
	public static Sprite getSprite(String res) {
		if (!Display.isCreated()) {
			throw new RuntimeException(
					"Attempt to retrieve sprite before game window was created");
		}

		return new Sprite(res);
	}

	/**
	 * Assigns a texture to a sprite and returns the sprite.
	 * Will load the texture if not yet loaded.
	 * @param res location of the texture to find.
	 * @return the sprite.
	 */
	public static Sprite getOrLoadSprite(String res, boolean collidable) {
		if (!Display.isCreated()) {
			throw new RuntimeException(
					"Attempt to retrieve sprite before game window was created");
		}

		return new Sprite(res, collidable);
	}

	public static void loadTexture(String res, boolean collidable) throws IOException {
		textureLoader.loadTexture(res, collidable);
	}

	/**
	 * Gets a texture from an image file.
	 * Gets the texture from the file system or retrieves it from map if already existant.
	 * @param res the file to retrieve the image.
	 * @param collidable whether the texture is pixel-perfect collidable.
	 * @return the texture
	 * @throws IOException
	 */
	public static Texture getOrLoadTexture(String res, boolean collidable) throws IOException {
		return textureLoader.getOrLoadTexture(res, collidable);
	}

	/**
	 * Gets an already loaded texture from the texture table.
	 * @param res the name of the texture.
	 * @return the texture.
	 */
	public static Texture getTexture(String res) {
		return textureLoader.getTexture(res);
	}
}
